# -*- coding: utf-8 -*-
import requests
import json
from urllib import parse
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators import csrf
import sys
import mysql.connector

def connect():
    conn = mysql.connector.connect(host='localhost', user='root', password='root', database='DjangoWeb')
    return conn

def save(tabelname,data):
    conn=connect()
    cursor=conn.cursor()
    value='("{}","{}")'.format(data['user'],data['password'])
    cursor.execute('insert into {} values {}'.format(tabelname,value))
    conn.commit()
    cursor.close()
    conn.close()
def find(tabelname,data):
    conn=connect()
    cursor=conn.cursor()
    value='"{}"'.format(data['user'])
    cursor.execute('select password from {} where user={}'.format(tabelname, value))
    values = cursor.fetchall()
    cursor.close()
    conn.close()
    return values

def search_post(request):
    list = []
    if request.POST:
        search_word = request.POST['q']
        conn = mysql.connector.connect(host='localhost', user='root', password='root', database='DjangoWeb')
        cursor = conn.cursor()
        cursor.execute('select * from course where concat(课程,教师,教室)like "%{}%"'.format(search_word))
        for i in cursor:
            temp = []
            try:
                for j in i:
                    temp.append(j)
                list.append(temp)
            except:
                pass
    if list == []:
        list = [[1, 1, 1], [1, 1, 1]]
    context = {}
    context['listindex'] = list
    return render(request, 'course.html', context)
